How to demonstrate the function returning object in PHP | PHP programs
We will demonstrate how we can return an object of a class from the function.
<?php
class Number
{
public $Num;
function __construct($n)
{
$this->Num = $n;
}
function Display()
{
echo "Num: " . $this->Num;
}
}
function AddObj($obj1, $obj2)
{
$temp = new Number(0);
$temp->Num = $obj1->Num + $obj2->Num;
return $temp;
}
$Obj1 = new Number(10);
$Obj2 = new Number(20);
$Obj3 = AddObj($Obj1, $Obj2);
$Obj3->Display();
?>


